﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;

namespace Lists
{
    public class SoTienMatTamChi : DoiTuong
    {
        #region Ngay property
        public NullableDate Ngay
        {
            get
            {
                return new NullableDate((DateTime)DataRow["Ngay"]);
            }
            set
            {
                DataRow["Ngay"] = value.Date;
            }
        }
        #endregion

        #region SoTien property
        private decimal _SoTien;
        public decimal SoTien
        {
            get
            {
                if (_DataRow["SoTien"] is decimal)
                    return (decimal)_DataRow["SoTien"];
                else
                    return 0;
            }
            set
            {
                _DataRow["SoTien"] = value;
            }
        }
        #endregion

        #region NguoiNhanTien property
        public string NguoiNhanTien
        {
            get
            {
                if (_DataRow["NguoiNhanTien"] is string)
                    return (string)_DataRow["NguoiNhanTien"];
                else
                    return "";
            }
            set
            {
                _DataRow["NguoiNhanTien"] = value;
            }
        }
        #endregion

        #region DienGiai property
        public string DienGiai
        {
            get
            {
                if (_DataRow["DienGiai"] is string)
                    return (string)_DataRow["DienGiai"];
                else
                    return "";
            }
            set
            {
                _DataRow["DienGiai"] = value;
            }
        }
        #endregion

        public SoTienMatTamChi(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucSoTienMatTamChi : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucSoTienMatTamChi _Instance;
        public static DanhMucSoTienMatTamChi Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucSoTienMatTamChi();
                    UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                    _Instance.DataTable.Constraints.Add(uniqueConstraint);
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "Ma";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "SoTienMatTamChi";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new SoTienMatTamChi((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public SoTienMatTamChi this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new SoTienMatTamChi(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public SoTienMatTamChi NewDoiTuong()
        {
            return new SoTienMatTamChi(DataTable.NewRow());
        }

        public static DanhMucSoTienMatTamChi Filter(string filterExpression)
        {
            DanhMucSoTienMatTamChi obj = new DanhMucSoTienMatTamChi();
            obj.DataView = new DataView(DanhMucSoTienMatTamChi.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
            return obj;
        }

        public static DanhMucSoTienMatTamChi GetByNgay(DateTime ngay)
        {
            DanhMucSoTienMatTamChi obj = new DanhMucSoTienMatTamChi();

            string filterEx = string.Format("'{0}-{1}-{2} 00:00:01' <= Ngay AND Ngay <= '{0}-{1}-{2} 23:23:59'", ngay.Year, ngay.Month, ngay.Day);
            obj = Filter(filterEx);

            return obj;
        }

        public SoTienMatTamChi GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new SoTienMatTamChi(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public SoTienMatTamChi GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucSoTienMatTamChi GetAll()
        {
            return Instance;
        }

        public DanhMucSoTienMatTamChi GetSoTienMatTamChiCon(Guid SoTienMatTamChiChaID)
        {
            try
            {
                string filterExpresstion = string.Format("SoTienMatTamChiChaID = '{0}'", SoTienMatTamChiChaID);
                return Filter(filterExpresstion);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
